package cn.net.yato.picture.util.phone;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author yato
 */
@Component
public class QcloudSmsUtil {

    @Value("${sms.appid}")
    private  int appid;

    @Value("${sms.appkey}")
    private  String appkey;

    @Value("${sms.templateId}")
    private  int templateId;

    @Value("${sms.smsSign}")
    private  String smsSign;

    public boolean sendSms(String[] phoneNumbers,String[] params) {
        try {
            SmsMultiSender msender = new SmsMultiSender(appid, appkey);
            SmsMultiSenderResult result =  msender.sendWithParam("86", phoneNumbers,
                templateId, params, smsSign, "", "");
            if (result.result==0){
                return true;
            }
        } catch (HTTPException e) {
            // HTTP响应码错误
            e.printStackTrace();
        } catch (JSONException e) {
            // json解析错误
            e.printStackTrace();
        } catch (IOException e) {
            // 网络IO错误
            e.printStackTrace();
        }
        return false;
    }
}
